<template>
  <base-breadcrumb :title="titleName">
    <searchData
      :search-data="searchArr"
      @search="search"
    />

    <a-card :bordered="false">
      <!-- <title-name title="项目岗位列表" /> -->
      <div class="table-operator">
        <!--<a-button type="primary" icon="plus" @click="handleAdd">新增岗位</a-button>-->
        <!--<a-button  @click="handleDel">删除</a-button>-->
      </div>

      <list-table
        ref="table"
        :loading="loadingState.isTable"
        :row-key="record=>record.postId"
        :columns="columns"
        :request="loadData"
        :pagination="false"
        show-size-changer
        show-quick-jumper
        :show-pagination="true"
        :showPage="false"
      >
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <span
          slot="action"
          slot-scope="text, record"
        >
          <MoreButton>
            <a @click="handleSub(record)"
            v-btnPermission="[{code:'seex_pm_002_loo_02',ident:'prma_seex'},{code:'buco_pm_002_loo_02',ident:'prma_buco'}]"
            >查看</a>
            <a @click="handleEdit(record)"
            v-btnPermission="[{code:'seex_pm_002_edi_03',ident:'prma_seex'},{code:'buco_pm_002_edi_03',ident:'prma_buco'}]"
            >编辑</a>
          </MoreButton>
        </span>
      </list-table>

      <add-post
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        :title="title"
        @cancel="handleCancel"
        @ok="handleOk"
      />
      <find-post
        :visible="visibles"
        :loading="confirmLoading"
        :model="mdls"
        @cancel="handleCancels"
      />
    </a-card>
  </base-breadcrumb>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import { getProjectJobsList, getProjectJobsOne, addProjectJobs, editProjectJobs,getDataDict,delPosts } from '@/api/project/posts'

  import addPost from './addPost'
  import findPost from './findPost'
  import ARow from 'ant-design-vue/es/grid/Row'

  const columns = [
    {
      title: '序号',
      scopedSlots: { customRender: 'serial' }
    },
    {
      title: '项目岗位名称',
      dataIndex: 'jobName'

    },
    {
      title: '岗位类型',
      dataIndex: 'jobType',
    },
    {
      title: '岗位所属部门',
      dataIndex: 'orgName',
    },
    {
      title: '岗位人数限制(土建)',
      dataIndex: 'numberLimitEngineering',

    },
    {
      title: '是否可兼任(土建)',
      dataIndex: 'canBothEngineering',
    },
    {
      title: '是否必填(土建)',
      dataIndex: 'requiredEngineering',
    },
    {
      title: '岗位人数限制(专业) ',
      dataIndex: 'numberLimitProfession',
    },
    {
      title: '是否可兼任(专业)',
      dataIndex: 'canBothProfession',
    },
    {
      title: '是否必填(专业)',
      dataIndex: 'requiredProfession',
    },
    {
      title: '操作',
      dataIndex: 'action',
      width: 150,
      fixed:'right',
      scopedSlots: { customRender: 'action' }
    }
  ]

  export default {
    name:'a'+Date.now(),
    components: {
      addPost,
      findPost
    },
    data() {
      return {
        loadingState: {
          isTable: false
        },
        columns,
        datas:[],
        loadData:parameter => {
          const requestParameters = Object.assign({}, parameter, this.queryParam)
          return getProjectJobsList(requestParameters)
            .then(res => {
              return res
            })
        },
        title:'',
        searchArr: [
          // 搜索框
          {
            name: '岗位名称',
            value: 'jobName',
            type: 'input'
          },
          {
            name:'岗位类型',
            value:'jobTypeCode',
            type:'select',
            data:[],
          },

        ],
        projectList:{},
        titleName:'',
        // create model
        visible: false,
        visibles: false,
        confirmLoading: false,
        mdl: null,
        mdls: null,
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},


        selectedRowKeys: [],
        selectedRows: [],
        isEdit:0,
        limitData:{},
        ids:[]
      }
    },
    created() {
      // this.getProjectJobsList();
      this.getDataDict();
    },
    computed: {
      rowSelection() {
        return {
          selectedRowKeys: this.selectedRowKeys,
          selectedRows: this.selectedRows,   // 当前行所有数据
          onChange: this.onSelectChange
        }
      }
    },
    methods: {
      // getProjectJobsList(){
      //   this.loadingState.isTable = true
      //   getProjectJobsList(this.queryParam).then(res=>{
      //     this.loadingState.isTable = false
      //     if(res.code == 200 || res.data.code == 200){
      //       this.datas = res.data || []
      //     }
      //   }).catch(() => {
      //     this.loadingState.isTable = false
      //   })
      // },
      search(values){
        let that = this
        that.queryParam = values || {}
        this.$refs.table.refresh(true)
        // this.getProjectJobsList()
      },
      getDataDict(){
        getDataDict().then(res=>{
           this.limitData = res.data
           let  arr =[]
           let arrys = []
            arr.push({code:'',name:'全部'})
            arrys = arr.concat(res.data.jobType)

           this.searchArr[1].data = this.getLabelData(arrys)
        })
      },
      deletes(){},
      handleEdit(record) {
        this.visible = true
        this.title = '编辑岗位'
        this.mdl = { ...record }
        this.isEdit = 1
      },
      handleOk() {
        let that = this;
        const form = that.$refs.createModal.form

        form.validateFields((errors, values) => {
          if (!errors) {

            if (that.mdl.jobId !="" && that.mdl.jobId != null) {
              // 修改

              values.code = that.mdl.code
              values.jobId = that.mdl.jobId
              values.orgId = that.mdl.orgId
              that.confirmLoading = true
              editProjectJobs(values).then(res=>{
                that.confirmLoading = false
                if(res.code == 200 ){
                  that.visible = false
                  // 重置表单数据
                  form.resetFields()
                  // 刷新表格
                  // that.getProjectJobsList()
                  this.$refs.table.refresh(true)
                  that.$message.success('修改成功')
                }else{
                  that.$message.error('修改失败')
                  that.confirmLoading = false
                  that.visible = true
                }
              }).catch(err=>{
                that.confirmLoading = false
              })

            } else {
              // 新增
              values.code = that.mdl.code
              values.postId = that.mdl.postId
              that.confirmLoading = true
              addProjectJobs(values).then(res=>{
                that.confirmLoading = false
                if(res.code == 200){
                  that.visible = false
                  that.confirmLoading = false
                  // 重置表单数据
                  form.resetFields()
                  // 刷新表格
                  // that.getProjectJobsList(true)
                  this.$refs.table.refresh(true)
                  that.$message.success('编辑成功')
                }else{
                  that.$message.error('编辑失败')
                  that.confirmLoading = false
                  that.visible = true
                }
              }).catch(err=>{
                that.confirmLoading = false
              })
            }
          }
        })
      },
      handleCancel() {
        this.visible = false

      },
      handleCancels() {
        this.visibles = false
      },
      handleSub(record) {
        this.visibles = true
        this.mdls = {...record}
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
        let arr = []
        selectedRows.forEach(function(obj,i) {
          arr.push(obj.jobId)
        })
        this.ids = arr
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      },
    }
  }
</script>
